package com.jblv.frame.common.logger;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import org.apache.logging.log4j.spi.LoggerAdapter;
import ch.qos.logback.classic.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title LoggerAdapterSetter
 * @projectName jblv-frame
 * @packageName com.jblv.frame.common.logger
 * @description LoggerAdapterSetter
 * @date Create at 2020/5/11 10:01
 * @since jdk1.8
 */
public class LoggerAdapterSetter {
    private boolean isCustom = false;

    private boolean isLogback = true;

    private String loggerLevel;

    private boolean isAccessConfigCenter = false;

    private String appId;

    private String localHost;

    public boolean isLogback() {
        return this.isLogback;
    }

    public void setLogback(boolean isLogback) {
        this.isLogback = isLogback;
    }

    public boolean isCustom() {
        return this.isCustom;
    }

    public void setCustom(boolean isCustom) {
        this.isCustom = isCustom;
    }

    public String getLoggerLevel() {
        if (this.loggerLevel == null) {
            LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
            Logger root = loggerContext.getLogger("ROOT");
            this.loggerLevel =  root.getLevel().levelStr;
        }
        return this.loggerLevel;
    }

    public void setLoggerLevel(String loggerLevel) {
        this.loggerLevel = loggerLevel;
        LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
        Logger root = loggerContext.getLogger("ROOT");
        root.setLevel(Level.toLevel(loggerLevel));
        List<Logger> loggerList = loggerContext.getLoggerList();
        for (Logger logger : loggerList)
            logger.setLevel(Level.toLevel(loggerLevel));
//        Logger.setEnableLevel(LogLevel.TRACE);
//        for (LoggerAdapter logger : LoggerAdapterFactory.loggers.values())
//            logger.setLevel(loggerLevel);
//        for (Class<?> clazz : LoggerAdapterFactory.loggerLevels.keySet()) {
//            LoggerAdapter logger = LoggerAdapterFactory.loggers.get(clazz);
//            logger.setLevel(LoggerAdapterFactory.loggerLevels.get(clazz));
//        }
    }

    public String getAppId() {
        return this.appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getLocalHost() {
        return this.localHost;
    }

    public void setLocalHost(String localHost) {
        this.localHost = localHost;
    }

    public boolean isAccessConfigCenter() {
        return this.isAccessConfigCenter;
    }

    public void setAccessConfigCenter(boolean accessConfigCenter) {
        this.isAccessConfigCenter = accessConfigCenter;
    }
}

